//
//  DHContactViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/23.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHContactViewController.h"
#import "Address.h"
#import "DHAddressViewController.h"

@interface DHContactViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoenTF;
@property (weak, nonatomic) IBOutlet UITextField *addressTF;
@property (weak, nonatomic) IBOutlet UITextField *address_detailTF;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSafeAreaLayout;

@property (weak, nonatomic) IBOutlet UIView *labelView;
@property (weak, nonatomic) IBOutlet UITextField *labelTF;

@property (strong, nonatomic) Location *location;

@end

@implementation DHContactViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLabel.text = @"地址选择";
    
    if ([UIDevice systemVersion] < 11) {
        self.topSafeAreaLayout.constant = 64;
    }
    
    if (!self.isManager) {
        self.labelView.hidden = YES;
    }else{
        self.labelView.hidden = NO;
    }
    
    self.nameTF.text = self.address.name;
    self.phoenTF.text = [Login curLoginUser].phone;
    
    [self updateAddress];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gaodeaddressselected:) name:@"gaodeaddressselected" object:nil];
}

- (void)updateAddress{
    if (self.address) {
        self.addressTF.text = [NSString stringWithFormat:@"%@%@%@",self.address.province,self.address.city,self.address.district];
        self.address_detailTF.text = self.address.address;
    }else{
        self.address = [[Address alloc] init];
    }
    self.nameTF.text = self.address.name;
    self.phoenTF.text = [Login curLoginUser].phone;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"gaodeaddressselected" object:nil];
}

- (void)gaodeaddressselected:(NSNotification *)not{
    if (not){
        NSDictionary *dic = not.object;
        GeoCode *geoCode = [dic objectForKey:@"geoCode"];
        self.address.province = geoCode.province;
        self.address.city = geoCode.city;
        self.address.district = geoCode.district;
        self.address.address = [dic objectForKey:@"detailAddress"];
        
        self.location = [dic objectForKey:@"location"];
        
        
        [self updateAddress];
    }
}

- (IBAction)selectAddressAction:(id)sender {
    [self.nameTF resignFirstResponder];
    DHAddressViewController *addressController = [[DHAddressViewController alloc] init];
    [self.navigationController pushViewController:addressController animated:YES];
}

- (IBAction)ensureAction:(id)sender {
    if (self.nameTF.text.length == 0) {
        [NSObject showHudTipStr:@"请输入联系人姓名"];
        return;
    }
    if (self.addressTF.text.length == 0) {
        [NSObject showHudTipStr:@"请输入联系地址"];
        return;
    }
    if (self.address_detailTF.text.length == 0) {
        [NSObject showHudTipStr:@"请输入详细地址"];
        return;
    }
    if (self.isManager) {
        if (self.labelTF.text.length == 0) {
            [NSObject showHudTipStr:@"请输入地址标签"];
            return;
        }
    }
    self.address.name = self.nameTF.text;
    self.address.phone = self.phoenTF.text;
    self.address.accurate = self.address_detailTF.text;
    self.address.logistic = @"普通快递";
    self.address.address = self.address_detailTF.text;
    self.address.label = self.labelTF.text;
    
    if (self.isReturn) {
        postNWithObj(@"return_address", @{@"address":self.address});
    }else if (self.isManager){
        NSDictionary *parmas = @{@"address":self.address,
                                 @"location":self.location
                                 };
        postNWithObj(@"DHUserAddressesController", parmas);
    }else if (self.isLiangTi){
        postNWithObj(@"liangti", @{@"address":self.address});
    }else if (self.isBack){
        postNWithObj(@"subscribe_back", @{@"address":self.address});
    }else{
        postNWithObj(@"DHContactViewController", @{@"address":self.address});
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if (textField.tag == 1000) {
        self.address.name = self.nameTF.text;
    }
    if (textField.tag == 1001) {
        self.address.address = textField.text;
    }
    return YES;
}

@end
